﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;
using Telerik.Web.UI;

namespace TangPhu.Admin.Page
{
    public partial class ProductListImage : System.Web.UI.Page
    {

        private string sErr = "";
        public string strUploadFolder = "~/" + ConfigurationManager.AppSettings["UploadFolder"].ToString()+"Images/";
        public string Username;
        public static int ContentLength;
        private string name_image = "";
        private bool iflag = false;
        private long inewid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["productid"] != null)
            {
                hdProductID.Value = Session["productid"].ToString();
                LoadRadImage();
            }
            if (Session["newsid"] != null)
            {
                hdNewID.Value = Session["newsid"].ToString();
                LoadRadImage();
            }

        }

        protected void Button12_Click(object sender, EventArgs e)
        {
            string name_image = upload_file(hdProductID.Value.ToString());
            ListImageControl ctr = new ListImageControl();
            ListImageInfo info = new ListImageInfo();
            info.Name = txtName.Text;
            info.NameVN = txtNameVN.Text;
            info.IMG = name_image;
            if (hdProductID.Value != "")

                info.IDProducts = Convert.ToInt64(hdProductID.Value);
            else
                info.IDProducts = -1;
            if (hdNewID.Value != "")
                info.IDNews = Convert.ToInt32(hdNewID.Value);
            else
                info.IDNews = -1;
            ctr.Add(info, ref sErr);
            LoadRadImage();
        }
        protected void LoadRadImage()
        {
            ListImageControl ctr = new ListImageControl();
            DataTable dtimage = new DataTable();
            if (hdProductID.Value != "")

                dtimage = ctr.GetByPro(Convert.ToInt64(hdProductID.Value.ToString()), ref sErr);
      //      if (hdNewID.Value != "")
         //       dtimage = ctr.Get_ListALLImage_ByIDNews(Convert.ToInt32(hdNewID.Value.ToString()), ref sErr);

            if (dtimage.Rows.Count > 0)
            {
                thumbRotator.DataSource = dtimage;
                thumbRotator.DataBind();
                imagePreview.ImageUrl = strUploadFolder + "large" + dtimage.Rows[0]["IMG"].ToString();
                if (hdIDImage.Value == "")
                {
                    hdIDImage.Value = dtimage.Rows[0]["ID"].ToString();
                }

            }
            else
            {
                thumbRotator.DataSource = null;
                
                thumbRotator.DataBind();
                thumbRotator.Items.Clear();
                imagePreview.ImageUrl = "";
            }
        }

        protected string upload_file(String code)
        {
            FileUpload fupload = FileUpload_image.FindControl("FileUpload_image") as FileUpload;
            FileUpload fupload_large = FileUpload_image.FindControl("FileUpload_image_large") as FileUpload;
            HttpPostedFile postFile = fupload.PostedFile;

            ContentLength = Convert.ToInt32(ConfigurationManager.AppSettings["ContentLength"].ToString());
            if (fupload.HasFile == true)
            {
                if (postFile.ContentLength < ContentLength)
                {
                    try
                    {
                        string[] s = fupload.FileName.Split(new char[] { '.' });

                        string filename = DateTime.Now.ToFileTimeUtc() + code + "." + s[s.Length - 1];
                        string filename_large = "large" + DateTime.Now.ToFileTimeUtc() + code + "." + s[s.Length - 1];
                        string strImagePath = Server.MapPath(strUploadFolder + filename);
                        string strImagePath_large = Server.MapPath(strUploadFolder + filename_large);
                        fupload.SaveAs(strImagePath);
                        fupload_large.SaveAs(strImagePath_large);
                        imagePreview.ImageUrl = strUploadFolder + filename_large;
                        name_image = filename;
                    }
                    catch (Exception ex)
                    {
                        lblErr.Text = ex.Message;
                    }
                }
                else
                {
                    lblErr.Text = "Vui lòng chọn file ảnh có kích thước nhỏ hơn 200kb";
                }

            }
            else
            {

                lblErr.Text = " file đã tồn tại"; //"Vui lòng chọn file ảnh có kích thước nhỏ hơn 200kb";
            }
            return name_image;
        }

        protected void ShowImage(object sender, RadRotatorEventArgs e)
        {

            //set the image information
            ListImageControl img_ctr = new ListImageControl();

            DataTable dtimage = new DataTable();
            if (hdProductID.Value != "")

                dtimage = img_ctr.GetByPro(Convert.ToInt64(hdProductID.Value.ToString()), ref sErr);
     //       if (hdNewID.Value != "")
       //         dtimage = img_ctr.Get_ListALLImage_ByIDNews(Convert.ToInt32(hdNewID.Value.ToString()), ref sErr);

            imagePreview.ImageUrl = strUploadFolder + "large" + dtimage.Rows[e.Item.Index]["IMG"];

            hdIDImage.Value = dtimage.Rows[e.Item.Index]["ID"].ToString();

        }

        protected void Button14_Click(object sender, EventArgs e)
        {
            if (hdIDImage.Value != "")
            {
                ListImageControl img_ctr = new ListImageControl();
                img_ctr.Delete(Convert.ToInt32(hdIDImage.Value));
                LoadRadImage();
            }
            if (hdNewID.Value != "")
            {
                ListImageControl img_ctr = new ListImageControl();
                img_ctr.Delete(Convert.ToInt32(hdIDImage.Value));
                LoadRadImage();
            }
        }
    }
}
